package com.my.algorithmlearning.leetcode;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName TwoSumSolution
 * @Description TwoSumSolution
 * @Author wanghaiwei
 * @Date 2022/11/18 16:42
 */
public class TwoSumSolution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashTable = new HashMap<>();
        for (int i = 0, j = nums.length - 1; i <= j; i++,j--) {
            int diffNumFromBegin = target - nums[i];
            if (hashTable.containsKey(diffNumFromBegin)) {
                return new int[]{hashTable.get(diffNumFromBegin), i};
            }else {
                hashTable.put(nums[i], i);
            }
            int diffNumFromEnd = target - nums[j];
            if (hashTable.containsKey(diffNumFromEnd)) {
                return new int[]{hashTable.get(diffNumFromEnd), j};
            }else {
                hashTable.put(nums[j], j);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{2,7,11,15};
        int target = 9;
        TwoSumSolution solution = new TwoSumSolution();
        System.out.println(Arrays.toString(solution.twoSum(nums, target)));
        System.out.println(102);
    }
}
